/**
 * 
 */
package com.example.android_is2.ListenerMap;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.graphics.Canvas;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;

import com.example.android_is2.InfoGeoPoints;
import com.example.android_is2.MainActivity;
import com.example.android_is2.MapItemizedOverlay;
import com.example.android_is2.db.DBTracker;
import com.example.android_is2.pathDraw.TrackCodingAddr;
import com.example.android_is2.trackerGeoPoint.MapOverlayTracker;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

/**
 * @author KAIN-PC
 * 
 */
public class LocationListenerMapTracker extends LocationListenerMap {

  public static final String MAP_HERE = "I'm HERE";

  private GeoPoint point = null;
  private MapOverlayTracker itemizedOverlayDraw = null;
  private DBTracker dbTracker = null;
  private ArrayList<InfoGeoPoints> infotrackpoint = null;

  private int i = 0;
  private String tmpLocationName = "";

  private MapItemizedOverlay itemizedoverlayForTrack = null;

  // 4 ARGOMENTI
  public LocationListenerMapTracker(MapView mapView, List<Overlay> overlays,
      MapItemizedOverlay itemizedOverlay, MapOverlayTracker itemizedoverlay2) {
    // TODO Auto-generated constructor stub
    super(mapView, overlays, itemizedOverlay);
    this.setItemizedOverlayDraw(itemizedoverlay2);
    i = 0;

  }

  public LocationListenerMapTracker(MapView mapView, List<Overlay> overlays,
      MapItemizedOverlay itemizedoverlay_my, MapOverlayTracker itemizedoverlay, DBTracker dbTracker) {
    // TODO Auto-generated constructor stub
    this(mapView, overlays, itemizedoverlay_my, itemizedoverlay);
    this.setDbTracker(dbTracker);
    infotrackpoint = new ArrayList<InfoGeoPoints>();
  }

  public LocationListenerMapTracker(MapView mapView, List<Overlay> overlays,
      MapItemizedOverlay itemizedoverlay_my, MapOverlayTracker itemizedoverlay,
      MapItemizedOverlay itemizedoverlayForTrack, DBTracker dbTracker2) {
    // TODO Auto-generated constructor stub
    this(mapView,overlays,itemizedoverlay_my,itemizedoverlay,dbTracker2);
    this.setItemizedoverlayForTrack(itemizedoverlayForTrack);
  }

  public MapItemizedOverlay getItemizedoverlayForTrack() {
    return itemizedoverlayForTrack;
  }

  public void setItemizedoverlayForTrack(MapItemizedOverlay itemizedoverlayForTrack) {
    this.itemizedoverlayForTrack = itemizedoverlayForTrack;
  }

  public ArrayList<InfoGeoPoints> getInfotrackpoint() {
    return infotrackpoint;
  }

  public DBTracker getDbTracker() {
    return dbTracker;
  }

  public void setDbTracker(DBTracker dbTracker) {
    this.dbTracker = dbTracker;
  }

  public MapOverlayTracker getItemizedOverlayDraw() {
    return itemizedOverlayDraw;
  }

  public void setItemizedOverlayDraw(MapOverlayTracker itemizedOverlayDraw) {
    this.itemizedOverlayDraw = itemizedOverlayDraw;
  }

  @Override
  public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub
    // Aggiorna il marker della mappa
    mapView.invalidate();
    Date timestamp = new Date(location.getTime());
    Log.i(MainActivity.TAG + getClass(), "ONLOCATIONCHANGED_TIMESPAMP: " + timestamp.toString());
    if (location != null) {
      LocationListenerMap.updateLocationData(location);

      // Rimozione Overlay precedenti per aggiornamento
      // getItemizedOverlayDraw().clearOverlay();

      int geoLat = (int) (location.getLatitude() * 1E6);
      int geoLng = (int) (location.getLongitude() * 1E6);
      point = new GeoPoint(geoLat, geoLng);

      InfoGeoPoints tmp = new InfoGeoPoints(location.getLatitude(), location.getLongitude(), "", "");

      tmp = TrackCodingAddr.TranslateGeoPoint2String(tmp);

      infotrackpoint.add(tmp);
      getMapcontroller().animateTo(point);
      i = i + 1;

      OverlayItem overlayitemTrack = null;
      overlayitemTrack = new OverlayItem(point, "Point #: " + i, "Lat: "
          + Double.toString(LocationListenerMap.getRound(point.getLatitudeE6(), 5)) + "\nLng: "
          + Double.toString(LocationListenerMap.getRound(point.getLongitudeE6(), 5))
          + "\nLocation: " + tmp.getText());
      Log.d(MainActivity.TAG + getClass(), "tmpLOCATION: " + tmpLocationName + " - LOCATION: "
          + tmp.getText());
      if (tmpLocationName.matches(tmp.getText())) {
        Log.i("DEBUG", "1::");
      } else {
        Log.i("DEBUG", "2::");
        tmpLocationName = tmp.getText();
        getItemizedoverlayForTrack().addOverlay(overlayitemTrack);
      }

      getItemizedOverlayDraw().addOverlay(overlayitemTrack);
      getItemizedOverlayDraw().draw(new Canvas(), getMapView(), true);

      getOverlays().add(getItemizedOverlayDraw());
      getOverlays().add(getItemizedoverlayForTrack());

      Log.i(MainActivity.TAG, "TRACKER LAT: " + geoLat + " - LONG: " + geoLng + " - LOCATION: "
          + tmp.getText());
    }
  }

  @Override
  public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub
    super.onProviderDisabled(provider);
  }

  @Override
  public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub
    super.onProviderEnabled(provider);
  }

  @Override
  public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub
    super.onStatusChanged(provider, status, extras);

    Log.i(MainActivity.TAG + getClass(), "ONSTATUSCHANGED");
  }

}
